<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php 
	$ci = & get_instance();
	$tid = intval($_GET['tid']);
	$TOURNAMENT = $ci->tournament_entity->init("id", $tid);
	
	if(mCrypt($tid) != $_GET['scrid']){
		redirect(site_url());exit();
	}	 
	
	echo php2js();
?>

<div class="clrboth"></div>	

<div id="center_content">
	<div id="async_event_in_match">
	     <!--
		<div class="clrboth"></div>
		<?php //$this->load->view('cricket/cricket/countdown_timeend_of_tournament'); ?>
		<div class="clrboth"></div>	

		<div class="sepDIV"></div>
	   -->	
	
		<div class="clrboth"></div>
		<?php $this->load->view('cricket/cricket/tournament_predictions_events'); ?>
		<div class="clrboth"></div>	

		<div class="sepDIV"></div>
<!--		
		<div class="clrboth"></div>
		<?php //$this->load->view('cricket/cricket/upcoming_matches_of_tournament'); ?>
		<div class="clrboth"></div>	

		<div class="sepDIV"></div>
		
		<div class="clrboth"></div>
		<?php //$this->load->view('cricket/cricket/closed_matches_of_tournament'); ?>
		<div class="clrboth"></div>	
-->	 
	</div>
</div>

<script type="text/javascript">
	var event_ID = [];
	var opening_event = {};
	var manual_bet = {};
	google.setOnLoadCallback(function() {
		$('#sys_plugin_my_profile').removeClass('hidden');
		$('#sys_plugin_bet').removeClass('hidden');
		$('#sys_plugin_convertion').removeClass('hidden');
		
		$('#async_event_in_match .clickbet').each(function(i,o){
				var event_id = $(this).attr('rel');
				var p= new PHP_JS();
				p.array_push(event_ID,event_id);
			}
		);		
		 
		setInterval("loadEventInMatch()",20000);

	});
	
	
	function loadEventInMatch(){
		$opening_event_id = opening_event.id;
		if($opening_event_id){
			return ;
			opening_event.html = $('#direct_bet_'+opening_event.id).html();
		}
		
		$.get(BASE_URI+MODULE+'/contentload/loadEventInTournamentPredictions', {tid:__GET.tid, scrid:__GET.scrid}, function(res){
			$("#async_event_in_match").html(res);
			/**
			$('.countdown').each(function(i,o){
				var id = $(this).attr('id');
				var time = $(this).attr('rel');
				eval( "sys_time."+id+"="+time);
			});
			**/
/**		
			$.getScript( BASE_URI +MODULE+ '/contentload/loadConvertionData/?mid='+__GET.mid , function(data, textStatus){
				if(!isEmpty(convertEnable) && convertEnable== 'no' ){
					$('#sys_plugin_convertion').hide();
				}else{
					$('#sys_plugin_convertion').show();
				}
				$('#rate_status').html( sysconvertion.point+':'+sysconvertion.coin);
			});
**/			
			$opening_event_id = opening_event.id;
			if($opening_event_id){
				$('#direct_bet_'+$opening_event_id).removeClass('hidden');
				$('#direct_bet_'+$opening_event_id).html(opening_event.html);
				$('#direct_stake_'+$opening_event_id).calculateWinPointsGet_direct($opening_event_id,$('#direct_stake_'+$opening_event_id).attr('value'));
			}
			
			restoreManualBetValue();
			
			$('#async_event_in_match .clickbet').each(function(i,o){
				var event_id = $(this).attr('rel');
				var odds = $('#async_event_in_match .clickbet .event_id_'+event_id).text();
				var odds_bet = $('#sys_plugin_bet .event_id_'+event_id).text();
				if(odds_bet){
					$P = new PHP_JS();
					$s = $P.floatval(odds);
					$s2 = $P.floatval(odds_bet);
					if($s2 != $s){
						$('#sys_plugin_bet .event_id_'+event_id).text(odds).addClass('brown');
						var rel = $('#sys_plugin_bet .event_id_'+event_id).attr('rel');
						$('#event_'+rel).attr('rel',$s);	
						$('#stake_'+rel).calculateWinPointsGet(rel,$('#stake_'+rel).attr('value'));
					}
				}
				
				var p = new PHP_JS();
				if(! p.in_array(event_id,event_ID)){
					//$('.event_'+event_id).append('<div class="update_notice">Added to Bet Slip</div>');
					p.array_push(event_ID,event_id);
				}
				//setTimeout(function(){$('.update_notice').remove();},20000); 
			});
			
		});
	}
	
	function tournament_predict(event_id){
		$stake = $('#direct_stake_'+event_id).attr('value');
		$points = $('#direct_win_'+event_id).attr('value');
		if(PRE_QUESTION){
			jQuery('#hiddenElement').html(PRE_QUESTION);
			jQuery( "#hiddenElement" ).dialog({
				resizable: false,
				height:200,
				width: 350,
				//modal: true,
				buttons: {
					"Ok": function() {
						$( this ).dialog( "close" );
						$.post(BASE_URI+MODULE+'/async/direct_bet_tournament',{event_id:event_id,stake:$stake,points:$points},function(res){
							var result = jQuery.parseJSON(res);
							
							jQuery('#hiddenElement').html(result.msg);
							jQuery('#hiddenElement').dialog(
								{
									 width: 350,
									 resizable: false,
									 height:200 ,
									 beforeClose: function(event, ui) { 
										if(result.status == 'ok'){
											reload();
										}
									 },
									  buttons: {
										"Ok": function() {
											$( this ).dialog( "close" );
										}
									}
								}
							);
						});
					},
					Cancel: function() {
						$( this ).dialog( "close" );
					}
				}
			});
		}else{
			$.post(BASE_URI+MODULE+'/async/direct_bet_tournament',{event_id:event_id,stake:$stake,points:$points},function(res){
				var result = jQuery.parseJSON(res);
				
				jQuery('#hiddenElement').html(result.msg);
				jQuery('#hiddenElement').dialog(
					{
						 width: 350,
						 resizable: false,
						 height:200 ,
						 beforeClose: function(event, ui) { 
							if(result.status == 'ok'){
								reload();
							}
						 },
						buttons: {
									"Ok": function() {
										$( this ).dialog( "close" );
									}
								}
					}
				);
			});
		}	
	}
	
	function tournament_predict_manual(cat_id){
		$stake = $('#direct_stake_'+cat_id).attr('value');
		$points = $('#direct_win_'+cat_id).attr('value');
		$type = $('#direct_event_type_'+cat_id).attr('value');
		
		if($type =='range'){
			$from = $('#range_from_'+cat_id).attr('value');
			$to = $('#range_to_'+cat_id).attr('value');
			if(!isNaturalNumber($from)){
				$('#range_from_'+cat_id).addClass('error');
				return;
			}else{
				$('#range_from_'+cat_id).removeClass('error');
			}	
			if(!isNaturalNumber($to)){
				$('#range_to_'+cat_id).addClass('error');
				return;
			}else{
				$('#range_to_'+cat_id).removeClass('error');
			}
			$value = $from+'{[|SEPSTR|]}'+$to;				
		}else{
			$array_value = getValueInputArray('box_'+cat_id);
			$value = $array_value? $array_value.join('{[|SEPSTR|]}') : '';
		}
		if(PRE_QUESTION){
			jQuery('#hiddenElement').html(PRE_QUESTION);
			jQuery( "#hiddenElement" ).dialog({
				resizable: false,
				height:200,
				width: 350,
				//modal: true,
				buttons: {
					"Ok": function() {
						$( this ).dialog( "close" );
						$.post(BASE_URI+MODULE+'/async/direct_bet_manual_tournament',{cat_id:cat_id,stake:$stake,points:$points,value:$value},function(res){
							var result = jQuery.parseJSON(res);
							
							jQuery('#hiddenElement').html(result.msg);
							jQuery('#hiddenElement').dialog(
								{
									 width: 350,
									 resizable: false,
									 height:200 ,
									 beforeClose: function(event, ui) { 
										if(result.status == 'ok'){
											clearManualBetValue();
											reload();
										}
									 },
									 buttons: {
										"Ok": function() {
											$( this ).dialog( "close" );
										}
									}
								}
							);
						});
					},
					Cancel: function() {
						$( this ).dialog( "close" );
					}
				}
			});
		}else{
			$.post(BASE_URI+MODULE+'/async/direct_bet_manual_tournament',{cat_id:cat_id,stake:$stake,points:$points,value:$value},function(res){
				var result = jQuery.parseJSON(res);
				
				jQuery('#hiddenElement').html(result.msg);
				jQuery('#hiddenElement').dialog(
					{
						 width: 350,
						 resizable: false,
						 height:200 ,
						 beforeClose: function(event, ui) { 
							if(result.status == 'ok'){
								clearManualBetValue();
								reload();
							}
						 },
						buttons: {
							"Ok": function() {
								$( this ).dialog( "close" );
							}
						}
					}
				);
			});
		}
	}
</script>